package com.wyl.auth.controller;

import com.wyl.auth.config.security.SecurityUser;
import com.wyl.auth.dto.SysUserDto;
import com.wyl.auth.dto.SysUserRoleDto;
import com.wyl.auth.req.ResetPasswordReq;
import com.wyl.auth.req.SysUserReq;
import com.wyl.auth.service.SysUserService;
import com.wyl.auth.vo.CurrentUserVo;
import com.wyl.auth.vo.SysUserVo;
import com.wyl.auth.vo.UserPermissionVo;
import com.wyl.common.CommonPage;
import com.wyl.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @Description 系统用户
* @Author WuYiLong
* @Date 2024-12-21
*/
@Api(tags = "系统用户")
@RestController
@RequestMapping("sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "保存用户")
    @PostMapping(value = "saveUser")
    public ResponseData saveUser(@RequestBody SysUserDto sysUserDto) {
        sysUserService.saveUser(sysUserDto);
        return ResponseData.getSuccess();
    }

    @ApiOperation(value = "用户分页")
    @PostMapping(value = "pageUser")
    public ResponseData<CommonPage<SysUserVo>> pageUser(@RequestBody SysUserReq sysUserReq) {
        return ResponseData.create().getSuccessInstance(sysUserService.pageUser(sysUserReq));
    }

    @ApiOperation(value = "删除用户")
    @PostMapping(value = "deleteUserByIds")
    public ResponseData deleteUserByIds(@RequestBody List<Long> ids) {
        sysUserService.deleteUserByIds(ids);
        return ResponseData.getSuccess();
    }

    @ApiOperation(value = "导出用户")
    @PostMapping(value = "exportUser")
    public void exportUser(@RequestBody SysUserReq sysUserReq, HttpServletResponse response) {
        sysUserService.exportUser(sysUserReq, response);
    }

    @ApiOperation(value = "重置密码")
    @PostMapping(value = "resetPassword")
    public ResponseData<String> resetPassword(Long id) {
        return ResponseData.getSuccessInstance(sysUserService.resetPassword(id));
    }

    @ApiOperation(value = "获取当前登录用户")
    @GetMapping(value = "getCurrentUser")
    public ResponseData<CurrentUserVo> getCurrentUser() {
        return ResponseData.getSuccessInstance(sysUserService.getCurrentUser());
    }

    @ApiOperation(value = "分配角色")
    @PostMapping(value = "assignRole")
    public ResponseData assignRole(@RequestBody SysUserRoleDto dto) {
        sysUserService.assignRole(dto);
        return ResponseData.getSuccess();
    }

    @ApiOperation(value = "根据用户id获得角色列表")
    @GetMapping(value = "listRolesByUserId")
    public ResponseData<List<Long>> listRolesByUserId(Long userId) {
        return ResponseData.getSuccessInstance(sysUserService.listRolesByUserId(userId));
    }

    @ApiOperation(value = "个人中心-重置密码")
    @PostMapping(value = "resetPassword2")
    public ResponseData resetPassword(@RequestBody ResetPasswordReq req) {
        sysUserService.resetPassword(req);
        return ResponseData.getSuccess();
    }

}
